﻿using Engine;
using Game;
namespace Mekiasm
{
    public class MekCraftBook : ItemFlat
    {
        public static int Index;
        public CraftBookWidget CraftBookWidget;
        public MekCraftBook() : base("Mek合成配方谱", MekiasmInit.GetFlatTexture(MekiasmInit.R1, "mekcraft"))
        {
            
            
        }
        public override string GetDescription(int value)
        {
            return "可以查看Mek科技Mod的所有合成配方";
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            ComponentGui componentGui = componentMiner.ComponentPlayer.ComponentGui;
            if (CraftBookWidget == null) CraftBookWidget = new CraftBookWidget(componentMiner, raycast);
            int v = 0;
            if (componentGui.ModalPanelWidget == null)
            {
                IInventory inventory = null;
                componentGui.ModalPanelWidget = CraftBookWidget;
                if (raycast.HasValue)
                {
                    v = raycast.Value.Value;
                    var p = raycast.Value.CellFace.Point;
                    var b = componentMiner.Project.FindSubsystem<SubsystemBlockEntities>().GetBlockEntity(p.X, p.Y, p.Z);
                    if (b != null)
                    {
                        inventory = b.Entity.FindComponent<IInventory>();
                    }
                }
                CraftBookWidget.TerrainRaycastResult = raycast;
                CraftBookWidget.SetCraftInventory(inventory, v);
            }
            else componentGui.ModalPanelWidget = null;
            return true;
        }
    }
}
